package alg;

/**
 * @author:yuziji
 * @create: 2023-04-14 20:23
 * @description：判断链表是否有环
 */
public class CircleList {

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(0);
        head.next.next.next = new Node(-4);
        head.next.next.next.next = head.next;

        System.out.println(isCircle(head).value);
    }

    static Node isCircle(Node head){
        Node fast = head;
        Node slow = head;
        while(fast.next!=null && fast!=null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast==slow){

                return slow;
            }
        }
        return null;
    }


    static class Node{
        private int value;
        private Node next;
        public Node(int value){
            this.value = value;
        }
    }
}
